package com.handler;

import com.service.impl.someserviceImpl;
import com.service.someservice;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class invocationhandler implements InvocationHandler {
    private Object targer;

    public invocationhandler(Object targer) {
        this.targer = targer;
    }
    @Override
    public Object invoke(Object proxy, Method method,
                         Object[] args) throws Throwable {
        System.out.println(new SimpleDateFormat("yyyy年MM月dd日")
                .format(new Date()));
        if (targer instanceof someserviceImpl) {
            if (method.getName().equals("dosome")) {
                System.out.println("dosome1代理模式");
            }
            else {
                System.out.println("无代理");
            }
            Object res = null;
            res = method.invoke(targer, args);
            return res;
        }
        else
        {
            System.out.println("代理模式2");
            Object res = null;
            res = method.invoke(targer, args);
            return res;
        }
    }
}
